package com.example.myandroidtest.ui.thread;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.example.myandroidtest.databinding.ActivityThreadBinding;

import java.util.Timer;
import java.util.TimerTask;

public class ThreadActivity extends AppCompatActivity {

    ActivityThreadBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityThreadBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        initView();
    }

    Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            Log.d("ThreadActivity", "handleMessage: " + msg.what);
            if (msg.what == 1) {
                String s = (String) msg.obj;
                System.out.println("handler 接受到的信息" + s);
            }
            return false;
        }
    });

    private void initView() {
        binding.toolbar.ivBack.setOnClickListener(v -> finish());
        binding.toolbar.tvTitle.setText("线程");

        binding.btnSend.setOnClickListener(v -> {
            Message message = new Message();
            message.what = 1;
            message.obj = "hello";
            handler.sendMessage(message);
        });

        binding.btnHandlerPost.setOnClickListener(v -> {
            new Thread(() -> {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        System.out.println("run" + Thread.currentThread().getName());
                        binding.btnHandlerPost.setText("哈哈哈");
                    }
                });
            }).start();
        });

        binding.btnRunOnUiThread.setOnClickListener(v -> {
            new Thread(() -> {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        System.out.println("run" + Thread.currentThread().getName());
                        binding.btnRunOnUiThread.setText("哈哈哈1111");
                    }
                });
            }).start();
        });
    }

    void timer() {
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {

            }
        }, 1000);
    }


    class Worker extends Thread {
        Handler mHandler;

        @Override
        public void run() {
            super.run();
            Looper.prepare();

            mHandler = new Handler(new Handler.Callback() {
                @Override
                public boolean handleMessage(@NonNull Message msg) {
                    if (msg.what == 1) {
                        String s = (String) msg.obj;
                        System.out.println("handler 接受到的信息" + s);
                    }
                    return false;
                }
            });
            Looper.loop();
        }
    }
}